/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package simulator.statistics;

import bgu.dcr.az.vis.gui.VisGUI;
import java.util.Collection;
import java.util.HashMap;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

/**
 *
 * @author Eran
 */
public class Statistics {
    public static final int ACTIVITY_WORK = 0, ACTIVITY_FUN = 1, ACTIVITY_DRIVE = 2, ACTIVITY_HOME = 3, ACTIVITY_RECHARGE = 4;
    private static final String[] activities_names = {"Work", "Fun", "Drive", "Home", "Recharge"};
    private HashMap<String, StatisticsCollector> plCollector;
    private StatisticsCollector totalPLCollector;
    private StatisticsCollector[] activityCollector;
    
    private final int agents, ticks;
    private int sampling_rate = 5;
    
    public Statistics(){
        this.agents = Utility.SIMULATOR_AGENTS;
        this.ticks = Utility.SIMULATOR_TICKS;
        
        plCollector = new HashMap<>();
        totalPLCollector = new StatisticsCollector();
        
        activityCollector = new StatisticsCollector[5];
        for (int i = 0; i < activityCollector.length; i++) {
            activityCollector[i] = new StatisticsCollector();
        }
    }
    
    public void endTick(){
        for (StatisticsCollector collector : plCollector.values()) {
            collector.tick();
        }
        
        for (StatisticsCollector collector : activityCollector) {
            collector.tick();
        }
        
        totalPLCollector.tick();
        
    }
    
    public void addPLs(Collection<String> pls){
        for(String pl : pls){
            this.plCollector.put(pl, new StatisticsCollector());
        }
    }
    
    public void enterPL(int agentID, String pl, double currPower){
        this.totalPLCollector.add(agentID);
        
        this.plCollector.get(pl).add(agentID);
    }
    
    public void exitPL(int agentID, String pl){
        this.totalPLCollector.remove(agentID);
        
        this.plCollector.get(pl).remove(agentID);
    }
    
    public void handleActivity(int activity, int agentID, boolean start){
        if(start){
            activityCollector[activity].add(agentID);
        }
        else{
            activityCollector[activity].remove(agentID);
        }
    }
    
    public void createCharts(){        
        constructPLsChart();
        constructTotalPLsChart();
        constructActivitiesChart();
    }

    private void constructActivitiesChart() {
        int c, i = 0;
        
        NumberAxis xAxis = new NumberAxis(), yAxis = new NumberAxis();
        xAxis.setLabel("Tick");
        yAxis.setLabel("People");
        LineChart<Number,Number> activitiesChart = new LineChart<>(xAxis,yAxis);
        activitiesChart.setTitle("People's Activity");

        for(StatisticsCollector activity : activityCollector){
            if("Recharge".equals(activities_names[i]))
                continue;
            XYChart.Series activitySeries = new XYChart.Series();
            activitySeries.setName(activities_names[i++]);
            
            c = 0;
            for(Integer sample : activity.getSamples())
                if(c++ % sampling_rate == 0)
                    activitySeries.getData().add(new XYChart.Data(c, sample));
            activitiesChart.getData().add(activitySeries);
        }
        
        activitiesChart.setCreateSymbols(false);
        VisGUI.charts.add(activitiesChart);
    }

    private void constructPLsChart() {
        int c, i = 0;
        
        NumberAxis xAxis = new NumberAxis(), yAxis = new NumberAxis();
        xAxis.setLabel("Tick");
        yAxis.setLabel("Cars");
        LineChart<Number,Number> plsChart = new LineChart<>(xAxis,yAxis);
        plsChart.setTitle("Parking-Lots' Utility");

        for(StatisticsCollector pl : plCollector.values()){
            XYChart.Series plSeries = new XYChart.Series();
            plSeries.setName("Lot " + (i++));
            
            c = 0;
            boolean onlyZeros = true;
            for(Integer sample : pl.getSamples()){
                onlyZeros = onlyZeros && (sample == 0);
                if(c++ % sampling_rate == 0)
                    plSeries.getData().add(new XYChart.Data(c, sample));
            }

            if(!onlyZeros)
                plsChart.getData().add(plSeries);
        }
        
        plsChart.setCreateSymbols(false);
        VisGUI.charts.add(plsChart);
    }

    private void constructTotalPLsChart() {
        int c;
        
        NumberAxis xAxis = new NumberAxis(), yAxis = new NumberAxis();
        xAxis.setLabel("Tick");
        yAxis.setLabel("Cars");
        LineChart<Number,Number> totalPLsChart = new LineChart<>(xAxis,yAxis);
        totalPLsChart.setTitle("Total Parking-Lots' Utility");

        XYChart.Series plSeries = new XYChart.Series();

        c = 0;
        for(Integer sample : totalPLCollector.getSamples())
            if(c++ % sampling_rate == 0)
                plSeries.getData().add(new XYChart.Data(c, sample));

        totalPLsChart.getData().add(plSeries);
        
        totalPLsChart.setCreateSymbols(false);
        VisGUI.charts.add(totalPLsChart);
    }
}
